$(function (){
   $("#uploadForm").submit(upload);
   $("#updateForm").submit(check_data);
   $("#old-password").focus(clear_error);
   $("#new-password").focus(clear_error);
   $("#confirm-password").focus(clear_error);
});
function  upload(){
    $.ajax({
        url:"http://upload-z1.qiniup.com",
        method:"post",
        processData:false,
        contentType:false,
        data:new FormData($("#uploadForm")[0]),
        success:function(data){
            if(data&& data.code==0){
                //更新头像访问路径
                $.post(
                    CONTXET_PATH+"/user/header/url",
                    {
                        "fileName":$("input[name='key']").val()
                    },
                    function (data){
                        data=$.parseJSON(data);
                        if(data.code==0){
                            window.location.reload();

                        }else{
                            alert(data.msg);
                       }
                    }
                );
            }else{
                alert("上传失败!");
            }
        }
    });
    return false;
}

function check_data() {
    var pwd1 = $("#new-password").val();
    var pwd2 = $("#confirm-password").val();
    if(pwd1 != pwd2) {
        $("#confirm-password").addClass("is-invalid");
        return false;
    }
    return true;
}

function clear_error() {
    $(this).removeClass("is-invalid");
}